package com.punai.dataanalysis.epdatakb.service.NG001change1212;

import com.punai.common.core.domain.BaseEntity;
import com.punai.common.utils.NumberUtil;
import com.punai.dataanalysis.epdatakb.bo.JtczResultBo;
import com.punai.dataanalysis.epdatakb.domain.DataAnalysisJyczbb;
import com.punai.dataanalysis.epdatakb.mapper.NG001change1212.JyCzBbC1212Mapper;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NG001JyCzBbService {

    @Autowired
    private JyCzBbC1212Mapper jyCzBbC1212Mapper;

    public List<? extends BaseEntity> getDataList(DataAnalysisVo dataAnalysisVo) {
        Map<String,Object> params = new HashMap<>();
        params.put("fgs",dataAnalysisVo.getFgs());
        params.put("month1",dataAnalysisVo.isMonth1());
        List<DataAnalysisJyczbb> infos = jyCzBbC1212Mapper.getInfos(params);
        infos = new ArrayList<>(infos);
        List<JtczResultBo> glrysl = jyCzBbC1212Mapper.glrysl();
        List<JtczResultBo> bncb = jyCzBbC1212Mapper.bncb(params);
        List<JtczResultBo> bncbjgc = jyCzBbC1212Mapper.bncbjgc();
        List<JtczResultBo> jzcb = jyCzBbC1212Mapper.jzcb();

        Map<String, BigDecimal> glryslMap = glrysl.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        Map<String, BigDecimal> bncbMap = bncb.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        Map<String, BigDecimal> bncbjgcMap = bncbjgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        Map<String, BigDecimal> jzcbMap = jzcb.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));

        for (DataAnalysisJyczbb info : infos) {
            String pc = info.getPc();
            BigDecimal glryslVal = glryslMap.getOrDefault(pc, BigDecimal.ZERO);
            BigDecimal bncbVal = bncbMap.getOrDefault(pc, BigDecimal.ZERO);
            BigDecimal bncbjgcVal = bncbjgcMap.getOrDefault(pc, BigDecimal.ZERO);
            BigDecimal jzcbVal = jzcbMap.getOrDefault(pc, BigDecimal.ZERO);

            info.setGlryrs(glryslVal);
            info.setBenYearCost(bncbVal);
            info.setBenYearJgc(bncbjgcVal);
            info.setNodeBeforeCb(jzcbVal);
            calcXjZb(info);
        }
        DataAnalysisJyczbb zj = zj(infos);
        zj.setProName("总合计");
        infos.add(zj);
        return infos;
    }

    private void calcXjZb(DataAnalysisJyczbb jy) {

        // excel上 未标注算法 和之前的表单一样把
        BigDecimal qnYsHj = jy.getYearYs();
        BigDecimal qnYsJgc = jy.getYearJgc();
        BigDecimal qnCbHj = jy.getYearCost();
        BigDecimal qnLr = NumberUtil.subtract(qnYsHj, qnCbHj);
        jy.setYearLr(qnLr);
        jy.setYearLrl(NumberUtil.divideUseProportion(qnLr,2,NumberUtil.add(qnYsHj,qnYsJgc)));

        // 本年
        BigDecimal currYearCzYs =  jy.getYearCzYs();
        BigDecimal currYearCzSj =  jy.getYearCzSj();
        BigDecimal currYearCzJgc = jy.getYearCzJgc();
        BigDecimal currYearCb =    jy.getBenYearCost();
        jy.setYearCzSubTotal(NumberUtil.adds(currYearCzYs,currYearCzSj,currYearCzJgc));
        BigDecimal currYearLr = NumberUtil.subtract(currYearCzYs, currYearCb);
        jy.setBenYearLr(currYearLr);
        jy.setBenYearLrl(NumberUtil.divideUseProportion(currYearLr,2,NumberUtil.add(currYearCzYs,currYearCzJgc)));

        // 累计工程利润
        BigDecimal nzYsHj = jy.getNodeBeforeYs();
        BigDecimal nzYsJgc = jy.getNodeBeforeYsJgc();
        BigDecimal nzCbHj = jy.getNodeBeforeCb();
        BigDecimal nzLr = NumberUtil.subtract(nzYsHj, nzCbHj);
        jy.setKlLr(nzLr);
        jy.setKlLrl(NumberUtil.divideUseProportion(nzLr,2,NumberUtil.add(nzYsHj,nzYsJgc)));

    }

    private DataAnalysisJyczbb zj(List<DataAnalysisJyczbb> res) {
        DataAnalysisJyczbb zj = new DataAnalysisJyczbb();
        BigDecimal htTotal = BigDecimal.ZERO;
        BigDecimal jzmj = BigDecimal.ZERO;
        BigDecimal glryrs = BigDecimal.ZERO;
        BigDecimal sgtYs = BigDecimal.ZERO;
        BigDecimal sgtJgc = BigDecimal.ZERO;
        BigDecimal yearYs = BigDecimal.ZERO;
        BigDecimal yearJgc = BigDecimal.ZERO;
        BigDecimal yearSj = BigDecimal.ZERO;
        BigDecimal yearCost = BigDecimal.ZERO;
        BigDecimal yearCostJgc = BigDecimal.ZERO;
        BigDecimal yearLr = BigDecimal.ZERO;
        BigDecimal yearCzYs = BigDecimal.ZERO;
        BigDecimal yearCzJgc = BigDecimal.ZERO;
        BigDecimal yearCzSj = BigDecimal.ZERO;
        BigDecimal yearCzSubTotal = BigDecimal.ZERO;
        BigDecimal yearCzTj = BigDecimal.ZERO;
        BigDecimal yearCzAz = BigDecimal.ZERO;
        BigDecimal yearCzQt = BigDecimal.ZERO;
        BigDecimal yearCzZyfb = BigDecimal.ZERO;
        BigDecimal benYearCost = BigDecimal.ZERO;
        BigDecimal benYearJgc = BigDecimal.ZERO;
        BigDecimal benYearLr = BigDecimal.ZERO;
        BigDecimal nodeBeforeYs = BigDecimal.ZERO;
        BigDecimal nodeBeforeYsJgc = BigDecimal.ZERO;
        BigDecimal nodeBeforeYsSj = BigDecimal.ZERO;
        BigDecimal nodeBeforeCb = BigDecimal.ZERO;
        BigDecimal nodeBeforeCbJgc = BigDecimal.ZERO;
        BigDecimal klLr = BigDecimal.ZERO;
        BigDecimal fzcCostFee = BigDecimal.ZERO;
        for (DataAnalysisJyczbb re : res) {
            htTotal = NumberUtil.add(htTotal,re.getHtTotal());
            jzmj = NumberUtil.add(jzmj,re.getJzmj());
            glryrs = NumberUtil.add(glryrs,re.getGlryrs());
            sgtYs = NumberUtil.add(sgtYs,re.getSgtYs());
            sgtJgc = NumberUtil.add(sgtJgc,re.getSgtJgc());
            yearYs = NumberUtil.add(yearYs,re.getYearYs());
            yearJgc = NumberUtil.add(yearJgc,re.getYearJgc());
            yearSj = NumberUtil.add(yearSj,re.getYearSj());
            yearCost = NumberUtil.add(yearCost,re.getYearCost());
            yearCostJgc = NumberUtil.add(yearCostJgc,re.getYearCostJgc());
            yearLr = NumberUtil.add(yearLr,re.getYearLr());
            yearCzYs = NumberUtil.add(yearCzYs,re.getYearCzYs());
            yearCzJgc = NumberUtil.add(yearCzJgc,re.getYearCzJgc());
            yearCzSj = NumberUtil.add(yearCzSj,re.getYearCzSj());
            yearCzSubTotal = NumberUtil.add(yearCzSubTotal,re.getYearCzSubTotal());
            yearCzTj = NumberUtil.add(yearCzTj,re.getYearCzTj());
            yearCzAz = NumberUtil.add(yearCzAz,re.getYearCzAz());
            yearCzQt = NumberUtil.add(yearCzQt,re.getYearCzQt());
            yearCzZyfb = NumberUtil.add(yearCzZyfb,re.getYearCzZyfb());
            benYearCost = NumberUtil.add(benYearCost,re.getBenYearCost());
            benYearJgc = NumberUtil.add(benYearJgc,re.getBenYearJgc());
            benYearLr = NumberUtil.add(benYearLr,re.getBenYearLr());
            nodeBeforeYs = NumberUtil.add(nodeBeforeYs,re.getNodeBeforeYs());
            nodeBeforeYsJgc = NumberUtil.add(nodeBeforeYsJgc,re.getNodeBeforeYsJgc());
            nodeBeforeYsSj = NumberUtil.add(nodeBeforeYsSj,re.getNodeBeforeYsSj());
            nodeBeforeCb = NumberUtil.add(nodeBeforeCb,re.getNodeBeforeCb());
            nodeBeforeCbJgc = NumberUtil.add(nodeBeforeCbJgc,re.getNodeBeforeCbJgc());
            klLr = NumberUtil.add(klLr,re.getKlLr());
            fzcCostFee = NumberUtil.add(fzcCostFee,re.getFzcCostFee());
        }
        zj.setHtTotal(htTotal);
        zj.setJzmj(jzmj);
        zj.setGlryrs(glryrs);
        zj.setSgtYs(sgtYs);
        zj.setSgtJgc(sgtJgc);
        zj.setYearYs(yearYs);
        zj.setYearJgc(yearJgc);
        zj.setYearSj(yearSj);
        zj.setYearCost(yearCost);
        zj.setYearCostJgc(yearCostJgc);
        zj.setYearLr(yearLr);
        zj.setYearCzYs(yearCzYs);
        zj.setYearCzJgc(yearCzJgc);
        zj.setYearCzSj(yearCzSj);
        zj.setYearCzSubTotal(yearCzSubTotal);
        zj.setYearCzTj(yearCzTj);
        zj.setYearCzAz(yearCzAz);
        zj.setYearCzQt(yearCzQt);
        zj.setYearCzZyfb(yearCzZyfb);
        zj.setBenYearCost(benYearCost);
        zj.setBenYearJgc(benYearJgc);
        zj.setBenYearLr(benYearLr);
        zj.setNodeBeforeYs(nodeBeforeYs);
        zj.setNodeBeforeYsJgc(nodeBeforeYsJgc);
        zj.setNodeBeforeYsSj(nodeBeforeYsSj);
        zj.setNodeBeforeCb(nodeBeforeCb);
        zj.setNodeBeforeCbJgc(nodeBeforeCbJgc);
        zj.setKlLr(klLr);
        zj.setFzcCostFee(fzcCostFee);
        zj.hb();
        calcXjZb(zj);
        return zj;
    }


}
